package com.masterclient.app.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.eoner.baselibrary.bean.goods.ProductBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.modules.goods.adapter.SignInRecyclerAdapter;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2019/12/17.
 */
public class SignInDialog extends Dialog {

    @BindView(R.id.tv_des)
    TextView tvDes;
    @BindView(R.id.recycler)
    RecyclerView recyclerView;

    private Context context;
    private SignInRecyclerAdapter signInRecyclerAdapter;

    public SignInDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public void setData(String sign_bupiao, String sign_integral, String sign_coupon, List<ProductBean> productBeanList) {
        String signIntegral = "";
        String signBuPiao = "";
        String signCoupon = "";
        if (!TextUtils.isEmpty(sign_integral) && Double.parseDouble(sign_integral) >0){
            signIntegral = "金币+"+sign_integral;
        }
        if (!TextUtils.isEmpty(sign_bupiao) && Double.parseDouble(sign_bupiao) >0){
            signBuPiao = "向日葵币+"+sign_bupiao;
        }
        if (!TextUtils.isEmpty(sign_coupon)){
            signCoupon =sign_coupon;
        }
        if (!TextUtils.isEmpty(signIntegral) && (!TextUtils.isEmpty(signBuPiao) || !TextUtils.isEmpty(signCoupon))){
            signIntegral = signIntegral+"/";
        }
        if (!TextUtils.isEmpty(signBuPiao) && !TextUtils.isEmpty(signCoupon)){
            signBuPiao = signBuPiao+"/";
        }
        tvDes.setText(String.format("已领取%s%s%s", signIntegral, signBuPiao, signCoupon));
        signInRecyclerAdapter.setNewData(productBeanList);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_sign_in);

        ButterKnife.bind(this);

        StaggeredGridNoScrollLayoutManager layoutManager = new StaggeredGridNoScrollLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(null);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(ScreenUtils.dp2px(2), 0, ScreenUtils.dp2px(3), 0);
            }
        });
        signInRecyclerAdapter = new SignInRecyclerAdapter();
        recyclerView.setAdapter(signInRecyclerAdapter);
        signInRecyclerAdapter.bindToRecyclerView(recyclerView);

        setCanceledOnTouchOutside(true);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowParams = Objects.requireNonNull(window).getAttributes();
        windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.TOP);
        window.setBackgroundDrawableResource(R.color.transparent);
        window.setAttributes(windowParams);
    }

    @OnClick({R.id.iv_close})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.iv_close) {
            dismiss();
        }
    }
}
